//
//  CZStatusOriginalView.swift
//  GZWeibo009
//
//  Created by gzxzmac on 16/3/25.
//  Copyright © 2016年 gzxzmac. All rights reserved.
//

import UIKit

import SnapKit
import YYKit

/// 原创微博的view
class CZStatusOriginalView: UIView {
    
    // MARK: - 属性
    
    /// 微博模型
    var status: CZStatus? {
        didSet {
            // 当别人来设置微博模型的时候,我们根据微博模型来设置原创微博的相关内容
            /// 1.设置头像
            if let imageUrl = status?.user?.profile_image_url {
                let url = NSURL(string: imageUrl)!
//                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"), completed: { (image, error, _, _) -> Void in
                    if error != nil {
                        print("error: \(error)")
                        return
                    }
                    
                    image.hm_asyncDrawImage(CGSize(width: 35, height: 35), isCorner: true, finished:  { (image) -> () in
                        self.iconView.image = image
                    })
                })
            }
            
            /// 设置名称
            nameLabel.text = status?.user?.screen_name
            
            /// 设置时间
            timeLabel.text = NSDate.dateWithSinaString(status?.created_at ?? "刚刚").sinaDateDescripiton()
            
            /// 来源
            sourceLabel.text = status?.source
            
            // 微博模型提供的数据不是很合理,需要view在显示的时候来计算需要的图片,如果其他地方也需要设置会员等级,又要这段重复的代码,在view即将显示的时来计算是需要消耗cpu,不要在cell的view里面来计算
            /// 会员等级, 直接找模型要图片
            memberView.image = status?.user?.mbrankImage
            
            /// 认证图标,直接找模型要图片
            verifiedView.image = status?.user?.verifiedTypeImage
            /// 设置微博内容
            contentLabel.attributedText = status?.attrText
            
//            let container = YYTextContainer(size: CGSize(width: UIScreen.width - 2 * CZStatusCellMargin, height: CGFloat(MAXFLOAT)))
//            
//            let layout = YYTextLayout(container: container, text: status!.attrText!)
//            contentLabel.textLayout = layout
            
            // 给picutreView赋值pictureURLs数组
            picutreView.pictureURLs = status?.storePictureURLs
            
            // 根据status是否有配图,重新设置底部约束
            picutreView.snp_updateConstraints(closure: { (make) -> Void in
                // 有图片显示到label底部的间距为CZStatusCellMargin
                // 没有图片显示到label底部的间距为0
                let margin = status?.storePictureURLs == nil ? 0 : CZStatusCellMargin
                make.top.equalTo(contentLabel.snp_bottom).offset(margin)
            })
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.whiteColor()
        prepareUI()
    }
    
    private func prepareUI() {
        // 添加子控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(memberView)
        addSubview(verifiedView)
        addSubview(contentLabel)
        addSubview(picutreView)
        
        // 添加约束
        /// 用户头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(CZStatusCellMargin)
            make.top.equalTo(self).offset(CZStatusCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        /// 用户名称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(CZStatusCellMargin)
            make.top.equalTo(iconView)
        }
        
        /// 时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(CZStatusCellMargin)
            make.bottom.equalTo(iconView)
        }
        
        /// 来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(CZStatusCellMargin)
            make.centerY.equalTo(timeLabel)
        }
        
        /// 会员等级
        memberView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(CZStatusCellMargin)
            make.centerY.equalTo(nameLabel)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        
        /// 认证图标
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
            make.size.equalTo(CGSize(width: 17, height: 17))
        }
        
        /// 微博文本内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(CZStatusCellMargin)
            make.leading.equalTo(self).offset(CZStatusCellMargin)
//            make.trailing.equalTo(self).offset(-CZStatusCellMargin)
        }
        
        /// 配图视图
        picutreView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(CZStatusCellMargin)
            make.leading.equalTo(self).offset(CZStatusCellMargin)
            make.size.equalTo(CGSize(width: 200, height: 200))
        }
        
        /// CZStatusOriginalView的底部参照picutreView的底部
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(picutreView).offset(CZStatusCellMargin).constraint
        }
    }

    // MARK: - 懒加载
    /// 用户头像
//    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
    private lazy var iconView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar"))
        
//        imageView.layer.cornerRadius = 17.5
//        imageView.layer.masksToBounds = true
        
        UIImage(named: "avatar")?.hm_asyncDrawImage(CGSize(width: 35, height: 35), isCorner: true, finished:  { (image) -> () in
                imageView.image = image
            })
        
        return imageView
    }()
    
    /// 用户名称
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(CZStatusCellContentSize)
        
        return label
    }()
    
    /// 时间
    private lazy var timeLabel: UILabel = {
        let label = UILabel()
        
        label.textColor = UIColor.orangeColor()
        label.font = UIFont.systemFontOfSize(CZStatusCellBottomDefaultFontSize)
        
        return label
    }()
    
    /// 来源
    private lazy var sourceLabel: UILabel = {
        let label = UILabel()
        
        label.textColor = UIColor.lightGrayColor()
        label.font = UIFont.systemFontOfSize(CZStatusCellBottomDefaultFontSize)
        
        return label
    }()
    
    /// 会员等级
    private lazy var memberView: UIImageView = UIImageView()
    
    /// 认证图标
    private lazy var verifiedView: UIImageView = UIImageView()
    
    /// 原创微博内容
    private lazy var contentLabel: FFLabel = {
        let label = FFLabel()
        
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(CZStatusCellContentSize)
        label.numberOfLines = 0
        
        // 换行/折行宽度
        label.preferredMaxLayoutWidth = UIScreen.width - 2 * CZStatusCellMargin
        
        label.labelDelegate = self
//        label.width = UIScreen.width - 2 * CZStatusCellMargin
//        label.textVerticalAlignment = YYTextVerticalAlignment.Top
//        label.displaysAsynchronously = true
//        label.ignoreCommonProperties = true
//        label.fadeOnAsynchronouslyDisplay = false
//        label.fadeOnHighlight = false
        
        return label
    }()
    
    /// 配图视图,不管cell是否要显示图片,先加载进来,如果模型里面不需要显示图片,就把这个配图视图隐藏掉,如果要显示图片就把这个配图视图显示出来
    private lazy var picutreView: CZStatusPictureView = CZStatusPictureView()
}

extension CZStatusOriginalView: FFLabelDelegate {
    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        CZPrint(msg: "点击的文字text: \(text)")
    }
}
